package cmd

import (
	"ReaderInOutLog/model"
	"ReaderInOutLog/proto"
	"ReaderInOutLog/util"
	"fmt"
	"time"
)

// 获取今天的时间调用list接口获取数据然后写入数据库
func SyncData(page int, size int, szjanitorName string) int {
	listReq := &proto.ReqReaderInOutLogAll{
		JanitorName: szjanitorName,
		Page:        page,
		Size:        size,
	}
	listData := GetList(listReq)
	lg.Debugf("SyncData listData Msg is %s Code is %d Total %d", listData.Msg, listData.Code, listData.Total)
	// model.ConnectDB()
	lg.Debug("SyncData ClearData")
	// defer model.CloseDB()
	lg.Debugf("SyncData at %v", listData.Datas)
	for _, v := range listData.Datas {
		lg.Debugf("SyncData add : %v", v)
		recordTimeStr, err := time.Parse(time.RFC3339, v.RecordTime)
		if err != nil {
			lg.Errorf("SyncData time.Parse error %v", err)
		}
		lg.Debugf("listData data RecordTime : %v", v.RecordTime)
		var recode = &model.ReaderInOutLog{
			Counter: v.Counter,
			// ID:          v.ID,
			JanitorName: v.JanitorName,
			ReaderID:    v.ReaderID,
			RecordTime:  recordTimeStr,
			Type:        v.Type,
		}
		model.InsertReaderInOutLog(recode)
	}
	return listData.Total
}

// 取当前时间
func RunSyncData() {
	model.ClearData()
	page := 1
	size := util.PageSize
	szjanitorName := util.SzIP
	lg.Debugf("SyncData szjanitorName at %s page %d size %d", szjanitorName, page, size)
	total := SyncData(page, size, szjanitorName)
	lg.Debugf("SyncData total at %d", total)
	for total > 0 {
		page++
		_ = SyncData(page, size, szjanitorName)
		// lg.Debugf("SyncData totala at %d", total)
		total = total - size
		lg.Debugf("SyncData totalb at %d", total)
	}
}

// 根据最新id取到应该上传的数据
func SyncDataByID() {
	// model.ConnectDB()
	// 获取最新的id
	lg.Debug("SyncDataByID running ")
	latestID, _ := model.GetMaxID()
	lg.Debugf("SyncDataByID latestID  is %v", latestID)
	// 获取环境变量ID
	maxID := GetMaxIDFromEnv()
	lg.Debugf("SyncDataByID maxID at %v", maxID)
	if latestID > maxID {
		recodes, _ := model.GetReaderInOutLogByIds(maxID)
		lg.Debugf("SyncDataByID recodes at %v", recodes)
		for _, v := range recodes {
			recordTimeStr := v.RecordTime.Format(time.RFC3339)
			var recode = &proto.ReqReaderInOutLog{
				ID:          v.ID,
				RecordTime:  recordTimeStr,
				JanitorName: v.JanitorName,
				ReaderID:    v.ReaderID,
				Type:        v.Type,
				Counter:     v.Counter,
			}
			AddRecode(recode)
		}
	} else {
		fmt.Println("没有需要上传的数据")
	}
	// defer model.CloseDB()
	SetMaxID(latestID)
}

func SyncDataTemp() {
	// model.ConnectDB()
	// 获取最新的id
	lg.Debug("SyncDataTemp running ")

	nids, err := model.GetReaderInOutLogTempIds()
	if err != 200 {
		lg.Errorf("SyncDataTemp GetReaderInOutLogTempIds error %v", err)
	}
	if len(nids) == 0 {
		fmt.Println("没有需要上传的数据")
		return
	}
	if len(nids) > 0 {
		lg.Debugf("SyncDataTemp nids at %v", nids)
		for _, nid := range nids {
			fmt.Println("有需要上传的数据", nid)
			lg.Debugf("SyncDataTemp nids at %v", nid)
			v, err := model.GetReaderInOutLogTempById(nid)
			if err != 200 {
				lg.Errorf("SyncDataTemp nid GetReaderInOutLogTempById error %v", err)
			}
			recordTimeStr := v.RecordTime.Format(time.RFC3339)
			var recode = &proto.ReqReaderInOutLog{
				ID:          v.ID,
				RecordTime:  recordTimeStr,
				JanitorName: v.JanitorName,
				ReaderID:    v.ReaderID,
				Type:        v.Type,
				Counter:     v.Counter,
			}
			AddRecode(recode)
			ids := model.DeleteReaderInOutLogTempById(nid)
			if ids != 200 {
				lg.Errorf("SyncDataTemp nid DeleteReaderInOutLogTempById error %v", err)
			}
		}
	}
}
